home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1997 / MacHack 1997.toast / Presentations / Presentations ’97 / Sessions ’97 / Multiplatform Code⁄Data Sharing / HelloBothWorlds / Libraries / gepicture.cpp < prev    next >
Encoding:
Text File  |  1997-06-26  |  3.3 KB  |  170 lines  |  [TEXT/CWIE]

  1.  
  2. // mail <chelly@eden.com> or surf http://www.eden.com/~chelly for feedback
  3. // free source code - do whatever you like with it
  4.  
  5. // uniform access to mac and universal pictures through common interface
  6.  
  7. #include "byteorder.h"
  8. #include "gepicture.h"
  9. #include "reschain.h"
  10. #include "GEOffscreen.h"
  11.  
  12. #include <string.h> // for memcpy()
  13.  
  14. // abstract base class
  15. struct GEPicture
  16. {
  17.     Rect m_bounds;
  18.     
  19.     virtual ~GEPicture() { }
  20.     virtual void Draw( const Rect* ) = 0;
  21. };
  22.  
  23. #if TARGET_IS_MACOS
  24.  
  25. #if 0
  26. #pragma mark ---------------------------------
  27. #pragma mark | mac picture type
  28. #pragma mark ---------------------------------
  29. #endif
  30.  
  31. struct GEPicture_macos : GEPicture
  32. {
  33.     PicHandle m_pic;
  34.     
  35.     GEPicture_macos( Handle pic );
  36.     ~GEPicture_macos();
  37.     virtual void Draw( const Rect* bounds );
  38. };
  39.  
  40. GEPicture_macos::GEPicture_macos( Handle pic )
  41. {
  42.     m_pic = (PicHandle) pic;
  43.     m_bounds = (**m_pic).picFrame;
  44.     OffsetRect( &m_bounds, -m_bounds.left, -m_bounds.top );
  45. }
  46.  
  47. GEPicture_macos::~GEPicture_macos()
  48. {
  49.     ReleaseResource( (Handle) m_pic );
  50. }
  51.  
  52. void GEPicture_macos::Draw( const Rect* r )
  53. {
  54.     DrawPicture( m_pic, r );
  55. }
  56.  
  57. #endif
  58.  
  59. #if 0
  60. #pragma mark ---------------------------------
  61. #pragma mark | universal picture type
  62. #pragma mark ---------------------------------
  63. #endif
  64.  
  65. // format of resource
  66. struct UnivPicData
  67. {
  68.     int16 h_size;
  69.     int16 v_size;
  70.     char  pixels [1];
  71. };
  72.  
  73. struct GEPicture_univ : GEPicture
  74. {
  75.     UnivPicData* m_resource;
  76.     char* m_pixels;
  77.  
  78.     GEPicture_univ( void* res );
  79.     ~GEPicture_univ();
  80.     virtual void Draw( const Rect* bounds );
  81. };
  82.  
  83. GEPicture_univ::GEPicture_univ( void* res )
  84. {
  85.     m_resource = (UnivPicData*) res;
  86.     
  87.     SwapIfRequired(&m_resource->h_size);
  88.     SwapIfRequired(&m_resource->v_size);
  89.  
  90.     m_bounds.top = m_bounds.left = 0;
  91.     m_bounds.right = m_resource->h_size;
  92.     m_bounds.bottom = m_resource->v_size;
  93.     m_pixels = m_resource->pixels;
  94.  
  95. #if TARGET_IS_WIN95    
  96.     // MUST remap black and white
  97.     unsigned char *tmp = (unsigned char *) m_pixels;
  98.     for (int i =m_bounds.bottom * m_bounds.right; i > 0; --i) {
  99.         switch (*tmp) {
  100.             case 0x00: *tmp = 255; break;
  101.             case 0xff: *tmp = 0; break;
  102.         }
  103.         tmp++;
  104.     }
  105. #endif
  106. }
  107.  
  108. GEPicture_univ::~GEPicture_univ()
  109. {
  110.     ReleaseResourcePtr( m_resource );
  111. }
  112.  
  113. void GEPicture_univ::Draw( const Rect* dest_bounds )
  114. {
  115.     CGrafPtr cur_port;
  116.     GDHandle cur_dev;
  117.     GetGWorld( &cur_port, &cur_dev );
  118.     GEOffscreenInfo offscreen = GetGEOffscreenInfo( cur_port );
  119.     
  120.     char* dest = offscreen.baseAddr;
  121.     char* src = m_pixels;
  122.     long src_row_bytes = m_bounds.right;
  123.     dest += dest_bounds->top * (long) offscreen.rowBytes + dest_bounds->left;
  124.     
  125.     for ( int v = m_bounds.bottom; v > 0; --v )
  126.     {
  127.         memcpy( dest, src, src_row_bytes );
  128.         dest += offscreen.rowBytes;
  129.         src += m_bounds.right; // no padding between lines (row bytes == width)
  130.     }
  131. }
  132.  
  133. #if 0
  134. #pragma mark ---------------------------------
  135. #pragma mark | interface
  136. #pragma mark ---------------------------------
  137. #endif
  138.  
  139. GEPicture* GetGEPicture( int res_id )
  140. {
  141.     void* res = GetResourcePtr( 'IMAG', res_id );
  142.     if ( res )
  143.         return new GEPicture_univ( res );
  144.     
  145. #if TARGET_IS_MACOS
  146.     // then use mac picture
  147.     Handle h = GetResource( 'PICT', res_id );
  148.     if ( h )
  149.         return new GEPicture_macos( h );
  150. #endif
  151.  
  152.     return nil;
  153. }
  154.  
  155. Rect    GetGEPictureFrame( GEPictureRef pic )
  156. {
  157.     return pic->m_bounds;
  158. }
  159.  
  160. void    DrawGEPicture( GEPictureRef pic, const Rect* rect )
  161. {
  162.     pic->Draw( rect );
  163. }
  164.  
  165. void    DisposeGEPicture( GEPictureRef pic )
  166. {
  167.     delete pic;
  168. }
  169.  
  170.